package com.dein.admin.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.dein.admin.model.Admin;
import com.dein.admin.service.AdminService;

@Controller
@RequestMapping("admin/")
public class AdminController {
	private AdminService adminService;
	
	public AdminService getAdminService(){
		return adminService;
	}
	
	@Autowired
	@Qualifier("adminService")
	public void setAdminService(AdminService adminService){
		this.adminService=adminService;
	}
	
	@RequestMapping(value="adminLogin", method=RequestMethod.POST)
	public @ResponseBody Object adminLogin(
			@RequestParam(value="adminName") String adminName,
			@RequestParam(value="adminPw") String adminPw,
			HttpServletRequest request) throws IOException{
		Map<String, Object> map=new HashMap<String, Object>();
		Long adminId=adminService.adminLogin(adminName, adminPw);
		if(adminId==null){
			//登录失败，密码或用户名错误
			map.put("result", false);
			return map;
		}
		Admin admin=adminService.getAdminById(adminId);
		if( admin==null){
			map.put("result", false);
			return map;
		}
		request.getSession().setAttribute("adminName", adminName);
		request.getSession().setAttribute("adminId", adminId);
		request.getSession().setAttribute("admin", admin);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping(value="amdinExist", method=RequestMethod.POST)
	public @ResponseBody Object adminExist(
			@RequestParam(value="adminName") String adminName){
		Map<String, Object> map=new HashMap<String, Object>();
		boolean result=adminService.adminExist(adminName);
        if(result){
            map.put("result", false);
            return map;
        }
        map.put("result", true);
        return map;	
	}
	/*@RequestMapping(value = "adminExit",method = RequestMethod.GET)
    public String adminExit(HttpServletRequest request){
        request.getSession().removeAttribute("adminName");
        request.getSession().removeAttribute("adminId");
        request.getSession().removeAttribute("admin");
        return "manager_login";
    }*/

}
